package com.corticon.client.model;

import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.lang.reflect.InvocationTargetException;

import com.corticon.client.util.ConvertSpecialUtil;

import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
import net.sf.ezmorph.bean.MorphDynaBean;

public class DialogReportOutput extends DialogReportBaseOutput {
	public List<ReportPageOutput> pages;

	public List<ReportPageOutput> getPages() {
		return pages;
	}

	public void setPages(List<ReportPageOutput> pages) {
		this.pages = pages;
	}

	public JSONObject toJson() {
		return JSONObject.fromObject(this);
	}

	private ConvertSpecialUtil covertSpecialUtil = new ConvertSpecialUtil();

	public DialogReportOutput toBean(String obj) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
		DialogReportOutput dialogReportOutput = (DialogReportOutput) JSONObject.toBean(JSONObject.fromObject(obj), this.getClass());
		List<ReportPageOutput> pages = dialogReportOutput.getPages();
		List<GeneralMessage> messages = dialogReportOutput.getErrorMessage();
		JSONArray jsonArrayMsg = JSONArray.fromObject(messages);
		dialogReportOutput.setErrorMessage(covertMessage(jsonArrayMsg.toString()));
		JSONArray jsonArray = JSONArray.fromObject(pages);
		// Collection<MorphDynaBean> mbs =
		// JSONArray.toCollection(JSONArray.fromObject(jsonArray.toString()));
		List newPages = this.covertQuestionBean(jsonArray.toString());
		dialogReportOutput.setPages(newPages);
		return dialogReportOutput;
	}

	public List<ReportPageOutput> covertQuestionBean(String jsonStr) {
		List<ReportPageOutput> returnObjects = new ArrayList();
		Collection<MorphDynaBean> mbs = JSONArray.toCollection(JSONArray.fromObject(jsonStr));

		for (Iterator<MorphDynaBean> rsIterator = mbs.iterator(); rsIterator.hasNext();) {
			MorphDynaBean mb = rsIterator.next();
			ReportPageOutput newObj = new ReportPageOutput();
			newObj.setDescription(mb.get("description").toString());
			newObj.setRefId(mb.get("refId").toString());
			newObj.setDisplayTitle(mb.get("displayTitle").toString());
			newObj.setHelpText(mb.get("helpText").toString());
			newObj.setName(mb.get("name").toString());
			newObj.setPageId(Integer.valueOf(mb.get("pageId").toString()));
			newObj.setText(mb.get("text").toString());
			newObj.setVersion(Integer.valueOf(mb.get("version").toString()));
			newObj.setVocabularyName(mb.get("vocabularyName").toString());
			newObj.setQuestion(covertQuestion(JSONArray.toCollection(JSONArray.fromObject(mb.get("question")))));
			newObj.setText(covertSpecialUtil.covertSpecialChar(newObj.getText()));
			newObj.setDisplayTitle(covertSpecialUtil.covertSpecialChar(newObj.getDisplayTitle()));
			newObj.setHelpText(covertSpecialUtil.covertSpecialChar(newObj.getHelpText()));
			newObj.setDescription(covertSpecialUtil.covertSpecialChar(newObj.getDescription()));
			returnObjects.add(newObj);
		}
		return returnObjects;
	}

	private List<QuestionDialogReportOutput> covertQuestion(Collection<MorphDynaBean> mbs) {
		List<QuestionDialogReportOutput> returnObjects = new ArrayList();
		// Collection<MorphDynaBean> mbs =
		// JSONArray.toCollection(JSONArray.fromObject(jsonStr));
		for (Iterator<MorphDynaBean> rsIterator = mbs.iterator(); rsIterator.hasNext();) {
			MorphDynaBean mb = rsIterator.next();
			QuestionDialogReportOutput newObj = new QuestionDialogReportOutput();
			String answerValue = "";
			String answerLabel = "";
			Object answerObj = mb.get("answerValue");
			Object answerLabelObj = mb.get("answerLabel");
			List answerArray = (List) answerObj;
			List answerLabelArray = (List) answerLabelObj;
			for (int i = 0; i < answerArray.size(); i++) {
				answerValue += mb.get("answerValue", i) + ";";
			}
			for (int i = 0; i < answerLabelArray.size(); i++) {
				String label = mb.get("answerLabel", i)==null?mb.get("answerValue", i).toString():mb.get("answerLabel", i).toString();				
				answerLabel += covertSpecialUtil.covertSpecialChar(label) + ";";				
			}
			newObj.setAnswerValue(answerValue.split(";"));
			newObj.setAnswerLabel(answerLabel.split(";"));
			newObj.setDataType(mb.get("dataType") == null ? null : mb.get("dataType").toString());
			newObj.setHelpText(mb.get("helpText").toString());
			newObj.setName(mb.get("name").toString());
			newObj.setResponseEntry(covertResponseEntryBean(mb.get("responseEntry")));
			newObj.setQuestionId(Integer.valueOf(mb.get("questionId").toString()));
			newObj.setRefId(mb.get("refId").toString());
			newObj.setResponseType(mb.get("responseType").toString());
			newObj.setSequence(mb.get("sequence").toString());
			newObj.setText(mb.get("text").toString());
			newObj.setUiControl(mb.get("uiControl").toString());
			newObj.setVersion(Integer.valueOf(mb.get("version").toString()));
			newObj.setText(covertSpecialUtil.covertSpecialChar(newObj.getText()));			
			newObj.setHelpText(covertSpecialUtil.covertSpecialChar(newObj.getHelpText()));
			
			returnObjects.add(newObj);
		}

		return returnObjects;
	}

	public List<GeneralMessage> covertMessage(String jsonStr) {
		List<GeneralMessage> messages = new ArrayList();
		Collection<MorphDynaBean> mbs = JSONArray.toCollection(JSONArray.fromObject(jsonStr));
		for (Iterator<MorphDynaBean> rsIterator = mbs.iterator(); rsIterator.hasNext();) {
			MorphDynaBean mb = rsIterator.next();
			GeneralMessage msg = new GeneralMessage();
			msg.setCode(mb.get("code").toString());
			msg.setQuestionId(Integer.valueOf(mb.get("questionId") == null ? null : mb.get("questionId").toString()));
			msg.setText(mb.get("text").toString());
			messages.add(msg);
		}

		return messages;
	}

	public List<ResponseEntryApiOutput> covertResponseEntryBean(Object morphDynaObj) {
		List<ResponseEntryApiOutput> returnObjects = new ArrayList();
		Collection<MorphDynaBean> mbs = JSONArray.toCollection(JSONArray.fromObject(morphDynaObj));
		for (Iterator<MorphDynaBean> rsIterator = mbs.iterator(); rsIterator.hasNext();) {
			MorphDynaBean mb = rsIterator.next();
			ResponseEntryApiOutput newObj = new ResponseEntryApiOutput();
			newObj.setLabel(mb.get("label").toString());
			newObj.setResponseEntryId(Integer.valueOf(mb.get("responseEntryId").toString()));
			newObj.setSequence(mb.get("sequence").toString());
			newObj.setValue(mb.get("value").toString());
			newObj.setLabel(covertSpecialUtil.covertSpecialChar(newObj.getLabel()));
			returnObjects.add(newObj);
		}
		return returnObjects;
	}
}